// pages/refun/refun.js
const api = require('../../utils/util.js');
const service = require('../../utils/service.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    reasonType: 1,
    userInfo: {},
    goodsInfo: {},
    reasonList: [],
    reason_id: '',
    showPon: false,
    showOther: false,
    showStatus: false,
    showPhotoSelect: false,
    statusText: '请选择',
    reasonText: '请选择',
    imgList: [],
    logistics: 1,
    remark: '',
    isSelectStatus: false,
    isSelectReason: false
  },

  onLoad: function (options) {
    let type = options.type // 1为退款  2为退货
    let order_id = options.order_id
    let userInfo = wx.getStorageSync('userInfo')
    this.setData({
      userInfo: userInfo,
      order_id: order_id,
      type: type
    })
    if (type == 2) {
      this.setData({
        showPhotoSelect: true
      })
    }
    this.init()
  },

  init: function () {
    // 退款商品信息
    let that = this
    api.ajax(service.url('orderDetail'), {
      unionid: that.data.userInfo.shop_unionid,
      order_id: that.data.order_id
    },
    function (res) {
      if (res.code == 0) {
        that.setData({
          goodsInfo: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
    this.getReasonList()
  },


  // 获取退款理由列表
  getReasonList: function (arg) {
    let type = this.data.type
    if (type == 1) {
      type = 2
    } else {
      type = 1
    }
    if (arg) {
      type = arg
    }
    let that = this
    api.ajax(service.url('getOrderReturnReasonList'), {
      type: type,
      order_id: that.data.order_id,
    },
    function (res) {
      if (res.code == 0) {
        that.setData({
          reasonList: res.data
        })
      } else {
        api.toast(res.message)
      }
    })
  },


  // 切换货物状态
  radioChangeS: function (e) {
    var checkVal = e.detail.value
    if (checkVal == 1) {
      // 未收到货
      this.data.statusText = '未收到货'
    } else if (checkVal == 2) {
      this.data.statusText = '已收到货'
    }

    this.setData({
      logistics: checkVal
    })

  },

  // 选择物流状态
  selectS: function () {
    if (this.data.statusText == '已收到货') {
      this.setData({
        showPhotoSelect: true
      })
      this.getReasonList(1)
    } else {
      this.setData({
        showPhotoSelect: false
      })
      this.getReasonList(2)
    }

    this.setData({
      statusText: this.data.statusText,
      showStatus: false,
      isSelectStatus: true
    })
  },

  // 获取单选框内容
  radioChange: function (e) {
    var checkVal = e.detail.value
    if(this.data.logistics == 1) {
      if (checkVal !== '21') {
        this.setData({
          showOther: false
        })
      } else {
        this.setData({
          showOther: true
        })
      }
    } else if(this.data.logistics == 2) {
      if (checkVal !== '20') {
        this.setData({
          showOther: false
        })
      } else {
        this.setData({
          showOther: true
        })
      }
    }

    this.setData({
      reason_id: checkVal
    })
  },

  // 选择理由
  select: function () {
    let arr = this.data.reasonList
    for (var i = 0, len = arr.length; i < len; i++) {
      if (arr[i].order_return_reason_id == this.data.reason_id) {
        this.data.reasonText = arr[i].content
      }
    }
    this.setData({
      reasonText: this.data.reasonText,
      showPon: false,
      isSelectReason: true
    })
  },

  // 获取退款说明
  getRemark: function (e) {
    this.setData({
      remark: e.detail.value,
    })
  },

  // 选择图片
  selectPhoto: function () {
    let that = this
    let imgarr = []
    wx.showActionSheet({
      itemList: ['从相册中选择', '拍照'],
      success: (res) => {
        if (res.tapIndex == 0) {
          api.upLoadImg(6, 'album', function (res) {
            that.setData({
              imgList: res
            })
          })
        } else {
          api.upLoadImg(6, 'camera', function (res) {
            that.setData({
              imgList: res
            })
          })
        }
      }
    })
  },

  // 删除图片
  delImg: function (e) {
    let that = this
    let idx = e.currentTarget.dataset.index
    this.data.imgList.splice(idx, 1)
    this.setData({
      imgList: that.data.imgList
    })
  },

  // 提交申请
  refundOrder: function () {
    let that = this
    let reason_id = this.data.reason_id
    let imgStr = ''
    let type = 1
    let goods_id = ''
    if (this.data.type == 1 && this.data.goodsInfo.order_state != 2 && this.data.goodsInfo.order_state != 7) {
      if (!this.data.isSelectStatus) {
        api.toast('请选择物流状态')
        return
      }
    }
    if (!this.data.isSelectReason) {
      api.toast('请选择退款理由')
      return
    }

    if (this.data.type == 1) {
      if (this.data.logistics == 1) {
        type = 1
      } else if (this.data.logistics == 2) {
        type = 2
        if (this.data.imgList.length <= 0) {
          api.toast('请上传凭证')
          return
        } else {
          for (var i = 0, len = this.data.imgList.length; i < len; i++) {
            imgStr += this.data.imgList[i] + ','
          }
        }
      }
    } else {
      type = 2
      if (this.data.imgList.length <= 0) {
        api.toast('请上传凭证')
        return
      } else {
        for (var i = 0, len = this.data.imgList.length; i < len; i++) {
          imgStr += this.data.imgList[i] + ','
        }
      }
    }

    if (this.data.goodsInfo.goods.length > 1) {
      for (var i = 0, len = this.data.goodsInfo.goods.length; i < len; i++) {
        goods_id += this.data.goodsInfo.goods[i].goods_id + ','
      }
    } else {
      goods_id = this.data.goodsInfo.goods[0].goods_id
    }

    api.ajax(service.url('refundOrder'), {
      unionid: that.data.userInfo.shop_unionid,
      order_id: that.data.order_id,
      order_return_reason_id: reason_id,
      type: type,
      refund_images: imgStr,
      remark: that.data.remark,
      goods_id: goods_id
    },
    function (res) {
      if (res.code == 0) {
        api.toast(res.message)
        setTimeout(function () {
          wx.redirectTo({
            url: `../refun_detail/refun_detail?refun_id=${res.data}&order_id=${that.data.order_id}`,
          })
        },1000)
      } else {
        api.toast(res.message)
      }
    })
  },

  // 显示隐藏弹层
  showPon: function () {
    this.setData({
      showPon: true
    })
  },
  hidePon: function () {
    this.setData({
      showPon: false
    })
  },
  // 显示、隐藏切换货物状态
  selectStatus: function () {
    this.setData({
      showStatus: true
    })
  },
  hideStatus: function () {
    this.setData({
      showStatus: false
    })
  }
})
